package fgl.common.config;

import com.alibaba.dubbo.config.*;
import com.alibaba.dubbo.config.spring.AnnotationBean;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.HashMap;
import java.util.Map;

/**
 * @author fuguangli
 * @description 前沿类
 * @Create date:    2017/7/4
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "dubbo", ignoreUnknownFields = false)
@PropertySource(value = "classpath:dubbo.properties")
public class DubboConfig {

    private String applicationName="application_p";
    private String applicationLogger="slf4j";
    private String annotationPackage="fgl.common.framework.dubbo,fgl";
    private String protocolName="dubbo";
    private Integer protocolPort=20880;
    private Boolean protocolAccessLog=true;
    private Integer providerTimeout=3000;
    private Integer providerRetries=1;
    private Integer provideDelay=-1;
    private String registryProtocol="multicast";
    private String registryAddress="224.0.1.47:6379";
    private Boolean registryRegister=true;
    private Boolean registrySubscribe=true;

    /**
     * 扫描dubbo包
     *
     * @return
     */
    @Bean
    public AnnotationBean annotationBean() {
        AnnotationBean bean = new AnnotationBean();
        bean.setPackage(annotationPackage);
        return bean;
    }

    /**
     * application info
     *
     * @return
     */
    @Bean
    public ApplicationConfig applicationConfig() {
        ApplicationConfig config = new ApplicationConfig();
        config.setName(applicationName);
        return config;
    }

    /**
     * register central
     */
    @Bean
    public RegistryConfig registryConfig() {
        RegistryConfig config = new RegistryConfig();
        config.setAddress(registryAddress);
        config.setProtocol(registryProtocol);
        Map<String,String> map = new HashMap<>();
        map.put("unicast","false");
        config.setParameters(map);
        return config;
    }

    /**
     * protocol configuration of service provider based on dubbo protocol
     */
    @Bean
    public ProtocolConfig protocolConfig() {
        ProtocolConfig config = new ProtocolConfig();
        config.setName(protocolName);
        config.setPort(protocolPort);
        config.setThreads(200);
        return config;
    }

    /**
     * service provider
     */
    @Bean
    public ProviderConfig defaultProvider(ApplicationConfig applicationConfig, RegistryConfig registryConfig, ProtocolConfig protocolConfig) {
        ProviderConfig config = new ProviderConfig();
        config.setTimeout(providerTimeout);
        config.setRetries(providerRetries);
        config.setDelay(provideDelay);
        config.setApplication(applicationConfig);
        config.setRegistry(registryConfig);
        config.setProtocol(protocolConfig);
        return config;
    }


    @Bean
    public ApplicationConfig consumerAppConfig() {
        ApplicationConfig config = new ApplicationConfig();
        config.setName("con");
        return config;
    }

    /**
     * Test Consumer
     */
    @Bean
    public ConsumerConfig consumerConfig(RegistryConfig registryConfig, ApplicationConfig consumerAppConfig) {
        ConsumerConfig config = new ConsumerConfig();
        config.setRegistry(registryConfig);
        config.setApplication(consumerAppConfig);

        return config;
    }


}
